/*
 * uart.h
 *
 *  Created on: Sep 19, 2013
 *      Author: Ken Arok
 */

#include "config_board.h"

#if BOARD_2_1_USED

#ifndef UART_H_
#define UART_H_

#include "compiler.h"

#define UART_NO_PARITY		0x00
#define UART_EVEN_PARITY	0x20
#define UART_ODD_PARITY		0x30

#define UART_1_STOPBIT		0x00
#define UART_2_STOPBIT		0x80

#define UART_5_DATABIT		0x00
#define UART_6_DATABIT		0x02
#define UART_7_DATABIT		0x04
#define UART_8_DATABIT		0x06
#define UART_9_DATABIT		0x08

//! Enumeration of serial port name.
typedef enum {
	serCOM1 = 0,
	serCOM2
}COM_port_t;

//! Status codes used by the UART driver.
typedef enum {
	UART_STATUS_OK				=  0, //!< Success
	UART_ERR_IO_ERROR			=  -1, //!< I/O error
	UART_ERR_FLUSHED			=  -2, //!< Request flushed from queue
	UART_ERR_TIMEOUT			=  -3, //!< Operation timed out
	UART_ERR_BAD_DATA			=  -4, //!< Data integrity check failed
	UART_ERR_PROTOCOL			=  -5, //!< Protocol error
	UART_ERR_UNSUPPORTED_DEV	=  -6, //!< Unsupported device
	UART_ERR_NO_MEMORY			=  -7, //!< Insufficient memory
	UART_ERR_INVALID_ARG		=  -8, //!< Invalid argument
	UART_ERR_BAD_ADDRESS		=  -9, //!< Bad address
	UART_ERR_BUSY				=  -10, //!< Resource is busy
	UART_ERR_BAD_FORMAT			=  -11, //!< Data format not recognized
	UART_OPERATION_IN_PROGRESS	= -128,
} uart_status_code_t;

typedef enum {
	UART_POLLED = 0,
	UART_POLLED_TX,
	UART_POLLED_RX,
	UART_INTERRUPT
} uart_transfer_mode_flag_t;


//! Input parameters when initializing RS232 and similar modes.
typedef struct
{
	uint32_t baudrate;			// Set baud rate of the UART.
	uint8_t charlength;			// Data width (5 to 9).
	uint8_t paritytype;			// Parity check.
	uint8_t stopbits;			// Stop bits between two characters
} uart_options_t;

/* Global signaling to wake up higher task. */
extern volatile bool xUART0Signaling, xUART1Signaling;

/** \brief Open the UART port.
 *
 * \param _xPort	The port communication want to be used.
 * \param _flag		Flag of opening.
 *
 * \return Status.
 *	\retval	UART_STATUS_OK		Port is ready.
 *	\retval UART_ERR_TIMEOUT	Port is not ready.
 *
 */
extern uart_status_code_t uart_open(COM_port_t _xPort, const uint8_t _flag);


/** \brief Close the UART port.
 *
 * \param _xPort		The port communication want to be used.
 *
 */
extern void uart_close(COM_port_t _xPort);

/** \brief Send a packet of word data out to serial port.
 *
 * \param _xPort		The port COM.
 * \param _pPacket		Pointer to the packet data want to be sent out.
 * \param _size_t		The length of packet want to be sent out.
 *
 * \return Status
 *	\retval UART_STATUS_OK		Success, data is being written.
 *	\retval UART_INVALID_ARG	Failed due to invalid port COM.
 *  \retval UART_ERR_TIMEOUT	Transmission timeout.
 */
extern uart_status_code_t uart_write_word(COM_port_t _xPort, const void *_pPacket, uint32_t _size_t);

/** \brief Send a packet of byte data out to serial port.
 *
 * \param _xPort		The port COM.
 * \param _pPacket		Pointer to the packet data want to be sent out.
 * \param _size_t		The length of packet want to be sent out.
 *
 * \return Status
 *	\retval UART_STATUS_OK		Success, data is being written.
 *	\retval UART_INVALID_ARG	Failed due to invalid port COM.
 *  \retval UART_ERR_TIMEOUT	Transmission timeout.
 */
extern uart_status_code_t uart_write_byte(COM_port_t _xPort, const void *_pPacket, uint32_t _size_t);

/** \brief Get a packet of word data from serial port.
 *
 * \param _xPort			The port COM.
 * \param _pRxBuff			Pointer to the buffer where the packet data will be stored.
 * \param _size_t			Number of word of data must be received.
 * \param _pRcvdSize		Pointer to the variable counting received data.
 *
 * \return Status
 *	\retval UART_STATUS_OK		Success, start capturing data.
 *	\retval UART_INVALID_ARG	Failed due to invalid port COM.
 *
 */
extern uart_status_code_t uart_read_word(COM_port_t _xPort, void *_pRxBuff, uint32_t _size_t, uint32_t *_pRcvdSize);

/** \brief Get a packet of byte data from serial port.
 *
 * \param _xPort			The port COM.
 * \param _pRxBuff			Pointer to the buffer where the packet data will be stored.
 * \param _size_t			Number of byte of data must be received.
 * \param _pRcvdSize		Pointer to the variable counting received data.
 *
 * \return Status
 *	\retval UART_STATUS_OK		Success, start capturing data.
 *	\retval UART_INVALID_ARG	Failed due to invalid port COM.
 *
 */
extern uart_status_code_t uart_read_byte(COM_port_t _xPort, void *_pRxBuff, uint32_t _size_t, uint32_t *_pRcvdSize);

/** \brief Initialize serial communication interface.
 */
extern void uart_init(void);

/** \brief Setting up UART communication.
 *
 * \param _xPort	The port COM.
 * \param _opt		UART option parameter.
 *
 */
extern void uart_setup(COM_port_t _xPort, const uart_options_t *_opt);

/** \brief Set UART transfer mode.
 *
 * \param _xPort	The port COM.
 * \param _flag		Mode flag.
 *					UART_POLLED: TX = Polled, RX = Polled.
 *					UART_POLEED_TX: TX = Polled, RX = Interrupt.
 *					UART_POLLED_RX: TX = Interrupt, RX = Polled.
 *					UART_INTERRUPT: TX = Interrupt, RX = Interrupt.
 *
 */
extern void uart_tranfer_mode(COM_port_t _xPort, uart_transfer_mode_flag_t _flag);


/** \brief Set timeout value to receiver.
 *
 * \param _xPort	The port COM.
 * \param _to_ms	Timeout value in milisecond (10 ms unit).
 *
 */
extern void uart_rx_timeout(COM_port_t _xPort, const uint32_t _to_ms);





#endif /* UART_H_ */


#endif /* BOARD_2_1_USED */
